package com.dissertation.service.impl;

import com.dissertation.model.ApplyAllow;
import com.dissertation.model.Assets;
import com.dissertation.model.Participant;
import com.dissertation.model.auth.Role;
import com.dissertation.model.auth.User;
import com.dissertation.respository.ApplyAllowRepository;
import com.dissertation.respository.AssetsRepository;
import com.dissertation.respository.ParticipantRepository;
import com.dissertation.service.IApplyAllowService;
import com.dissertation.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;


@Service
public class AuthChecker implements IApplyAllowService {

    @Autowired
    ApplyAllowRepository applyAllowRepository;

    @Autowired
    ParticipantRepository participantRepository;

    @Autowired
    AssetsRepository assetsRepository;


    @Override
    public boolean allow( String assetId ) {
        String name = AuthUtil.getUser().map(User::getName).orElse("");
        Assets assets = assetsRepository.findOne( assetId );
        List<ApplyAllow> applyAllowList = applyAllowRepository.findByApplyNameAndAssetIdAndStatusIsTrue( name, assetId);
        Date now = new Date();
        Boolean result = false;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (ApplyAllow e : applyAllowList) {
            try {
                Date dbtime = format.parse(e.getDeadtime());
                if (dbtime.getTime() > now.getTime()) {
                    result = true;
                }else{
                    e.setStatus(false);
                }
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        }
        return assets.getRelative().contains( name )
                || result;
    }

    @Override
    public boolean allow() {
        User user = AuthUtil.getUser().orElseThrow( () -> new RuntimeException( "请先登录" ) );
        Set<Role> roles = user.getRole();
        return roles.contains( Role.ADMIN );
    }

    @Override
    public boolean allow( Assets assets ) {
        if ( assets.getAuthor() == null ) {
            return false;
        }
        String name = getName();
        return assets.getAuthor().equals( name );
    }

    @Override
    public boolean adminOrTeacherAllow() {
        User user = AuthUtil.getUser().orElseThrow( () -> new RuntimeException( "请先登录" ) );
        Set<Role> roles = user.getRole();
        return roles.contains( Role.ADMIN ) || roles.contains( Role.TEACHER );
    }

    @Override
    public boolean allUserAllow() {
        User user = AuthUtil.getUser().orElseThrow( () -> new RuntimeException( "请先登录" ) );
        Set<Role> roles = user.getRole();
        return roles.contains( Role.ADMIN ) || roles.contains( Role.TEACHER ) || roles.contains( Role.STUDENT );
    }


    /**
     *
     * @return 可能 为null
     */
    private String getName() {
        String id = AuthUtil.getId();
        Participant participant = participantRepository.findByUserIdAndDeletedIsFalse( id ).orElse( new Participant() );
        return participant.getUniqueName();
    }
}
